package psd.model.Prolog.Converters;

import java.util.Iterator;
import java.util.List;

import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologVariableDeclarationExpressionConverter extends
		PrologConverter implements IConverter<VariableDeclarationExpression> {

	public PrologVariableDeclarationExpressionConverter(
			FactGenerationToolbox toolbox) {
		super(toolbox);
	}

	@Override
	public boolean convert(VariableDeclarationExpression node) {
		List fragments = node.fragments();
		int mods = node.getModifiers();
		VariableDeclarationFragment fragment;
		for (Iterator i = fragments.iterator(); i.hasNext(); writeModifiers(
				fragment, mods)) {
			fragment = (VariableDeclarationFragment) i.next();
			String args[] = {
					idResolver.getID(fragment),
					getParentId(node),
					idResolver.getID(getEnclosingNode(fragment)),
					typeResolver.getTypeTerm(fragment.resolveBinding()
							.getType()),
					(new StringBuilder("'")).append(
							fragment.getName().getIdentifier()).append("'")
							.toString(),
					idResolver.getID(fragment.getInitializer()), this.getFilePath(node), this.getLineNumber(node) };
			writer.writeFact("localDefT", args);
			writer.writeFact("slT", new String[] { idResolver.getID(fragment),
					Integer.toString(fragment.getStartPosition()),
					Integer.toString(fragment.getLength()), this.getFilePath(node), this.getLineNumber(node) });
			checkAndGenComments(node, idResolver.getID(fragment));
			writeSourceLocationArgumentIdentifier(fragment, fragment.getName(),
					node.modifiers());
		}

		return true;
	}

}
